package controller

import (
	"devops/code"
	"devops/controller/validation"
	"devops/service"

	"github.com/gin-gonic/gin"
	v20180326 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tsf/v20180326"
)

// @Summary 创建部署任务
// @Tags 持续部署
// @Produce  json
// @Param	body  body  validation.CreateCD  true  "创建参数"
// @Success 200 {object} data.SuccessCreateCD  	  		"成功返回"
// @Failure 10000 {object} data.ParamError     			"错误返回"
// @Failure 11000 {object} data.FailCreateCD     		"错误返回"
// @Router /api/cd/create [post]
func CreateCD(c *gin.Context) {
	var param validation.CreateCD
	if err := c.ShouldBind(&param); err != nil {
		ErrorResponse(c, code.PARAMETER_ERR, err.Error())
		return
	}
	devops := service.NewDevOps()
	err := devops.CreateCD(param.CID, param.GroupID)
	if err != nil {
		ErrorResponse(c, code.CREATE_FAIL, nil)
		return
	}
	SuccessResponse(c, "创建部署计划成功")
}

// @Summary 获取构建计划对应的部署任务
// @Tags 持续部署
// @Produce  json
// @Param	body  body  validation.GetCD  true  "创建参数"
// @Success 200 {object} data.SuccessGetCD  	  		"成功返回"
// @Failure 10000 {object} data.ParamError     			"错误返回"
// @Router /api/cd/one [get]
func GetCD(c *gin.Context) {
	var param validation.GetCD
	if err := c.ShouldBind(&param); err != nil {
		ErrorResponse(c, code.PARAMETER_ERR, err.Error())
		return
	}
	devops := service.NewDevOps()
	SuccessResponse(c, devops.GetCD(param.BuildID))
}

// @Summary 获取可用部署组
// @Tags 持续部署
// @Produce  json
// @Param	query  query validation.GetDeployGroups  true  "创建参数"
// @Success 200 {object} data.SuccessGetDeployGroups  	  		"成功返回"
// @Failure 10000 {object} data.ParamError     			"错误返回"
// @Failure 11003 {object} data.FailGetDeployGroups     			"错误返回"
// @Router /api/cd/deploy/groups [get]
func GetDeployGroups(c *gin.Context) {
	var param validation.GetDeployGroups
	if err := c.ShouldBind(&param); err != nil {
		ErrorResponse(c, code.PARAMETER_ERR, err.Error())
		return
	}
	devops := service.NewDevOps()
	groups, err := devops.GetContainerGroups(param.AppID)
	if err != nil {
		ErrorResponse(c, code.SELECT_FAIL, err.Error())
		return
	}
	content := groups.Response.Result.Content
	type deployGroup struct {
		*v20180326.ContainGroup
		Status string
	}
	deployGroups := make([]*deployGroup, 0, len(content))
	if len(content) > 0 {
		for _, c := range content {
			var status string
			if detail, err := devops.GetGroupDetail(*c.GroupId); err == nil {
				status = *detail.Response.Result.Status
			}

			deployGroups = append(deployGroups, &deployGroup{c, status})
		}

	}
	SuccessResponse(c, deployGroups)
}

// @Summary 获取应用列表
// @Tags 持续部署
// @Produce  json
// @Success 200 {object} data.SuccessGetApps  	  		"成功返回"
// @Failure 10000 {object} data.ParamError     			"错误返回"
// @Failure 11003 {object} data.FailGetDeployGroups     "错误返回"
// @Router /api/cd/deploy/apps [get]
func GetApps(c *gin.Context) {

	devops := service.NewDevOps()
	apps, err := devops.GetApplications("", "C")
	if err != nil {
		ErrorResponse(c, code.SELECT_FAIL, err.Error())
		return
	}
	SuccessResponse(c, apps.Response.Result.Content)
}
